function gotoadd(dom){
	var dialog = editAdminDataDialog("left,top",dom,label_add,"http://localhost:8080/adminconsole/General/ListOfCurrency/add.jsp","addDialog",AddEventClick,loadElementFinish,null);
}

function loadElementFinish(dialog){
	dialog.parents(".ui-dialog:first").css("width",600);
	fillAddDialog(dialog);
}

function fillAddDialog(dialog){
	var url = "/currencyService";
	var data = { func : "getCurrencyForDropDown"}
	simpleserviceSyn(url,data,fillDropDown,dialog);
}

function fillDropDown(data,dialog){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var usingCurrency = objArray["usingCurrency"];
	var allCurrency = objArray["allCurrency"];
	var defaultCurrency = objArray["defaultCurrency"];
	for ( var i = 0; i < allCurrency.length; i++) {
		dialog.find("#allCurrency").append(
				$("<option>").val(allCurrency[i]['id']).html(allCurrency[i]['title']));
		}
	
	for ( var i = 0; i < usingCurrency.length; i++) {
		dialog.find("#selectedCurrency").append(
				$("<option>").val(usingCurrency[i]['id']).html(usingCurrency[i]['title']));
		}
	dialog.find("#selectedCurrency option[value*='"+defaultCurrency+"']").prop('disabled',true);
}

function AddEventClick(dialog,id){
	waitingDialog({});
	if($("[name=maxEntries]").val() == null){
		var line = 10;
	}else{
		var line = $("[name=maxEntries]").val();
	}
	if($("[name=selectpage]").val() == null){
		var pagePointer = 1;
	}else{
		var pagePointer = $("[name=selectpage]").val();
	}
	var selectedCurrency= "";
	for(var i = 0;i<dialog.find("#selectedCurrency option").size();i++){
		if(i != dialog.find("#selectedCurrency option").size()-1){
			selectedCurrency += $(dialog.find("#selectedCurrency option")[0]).val()+",";
		}else{
			selectedCurrency += $(dialog.find("#selectedCurrency option")[0]).val();
		}
	}
		
	if(selectedCurrency == ""){
		closeWaitingDialog();
		return false;
	}else{
		var url = "/currencyService";
		var func = "editUsingCurrency";
		var offset =  (line*pagePointer)-line;
		var data = { func : func,
				offset : offset,
				line: line,
				pagePointer: pagePointer,
				id : selectedCurrency
			}
		simpleservice(url,data,showCurrencyList);
	}

}

function showCurrency(id){
	var objArray = new Array();
	if(id!=null&&id!='add'&&id!="next"&&id != "previous"){
		objArray = JSON.parse(id);
	}
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(id == null){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/currencyService";
	var data = { func: "getCurrency4Admin",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showCurrencyList);
}

function showCurrencyList(data){
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	var sumtext = "";
	var text = "";
	var pageAmount = objArray2["pageAmount"];
	var line = objArray2["lineAmount"];
	var pagePointer = objArray2["pageNumber"];
	var objArray = objArray2["data"];
	$("#listsize").html("&nbsp;("+objArray2["dataAmount"]+")");
	var lineNumber = (line * pagePointer) - (line-1) ;
	var defaultCurrency = objArray2["defaultCurrency"];
	if(objArray.length == 0){
		text = "<tr><td colspan=\"7\" style=\"border-top:1px #D6D7D5 solid;border-bottom:1px #D6D7D5 solid;\">"+nodata+"</td></tr>";
		sumtext += text;
	}else{
		for(var i = 0;i< objArray.length+1;i++){
				if(i != objArray.length ){
					var isDefault = false;
					if(objArray2["defaultCurrency"] == objArray[i]['id']){
						var isDefault = true;
					}
					text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
							"<td id=\"col_1\" align=\"center\"> "+(lineNumber+i)+". </td>"+
							"<td><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
							"<td align=\"center\"> "+objArray[i]['title']+" </td>"+
							"<td align=\"center\">"+objArray[i]['abreviation']+" </td>"+
							"<td align=\"center\">"+objArray[i]['symbol']+"</td>"+
							"<td align=\"center\">"+objArray[i]['id']+"</td>"+
							"<td align=\"center\"><a href=\"#\" onclick=\"ChangeSetToDefault("+objArray[i]['id']+")\">" +
							"<span id=\"isDefault"+objArray[i]['id']+"\" value=\""+isDefault+"\">"+isDefault+"</span></a></td>"+
							"</tr>";
					sumtext += text;
				}else{
					text = "<tr>"+
					"<td colspan=\"13\" style=\"padding:0;\">"+
					"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
					"<tbody>"+
						"<tr>"+
							"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
								"<select name=\"selectpage\" onchange=\"showCurrency()\">"+
								"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
							"</td>"+
							"<td width=\"31%\" align=\"center\">"+foor_view+
								"<select name=\"maxEntries\" onchange=\"showCurrency()\">"+
									"<option style=\"color:#000000\"  value=\"10\">10</option>"+
									"<option style=\"color:#000000\"  value=\"20\">20</option>"+
									"<option style=\"color:#000000\"  value=\"50\">50</option>"+
									"<option style=\"color:#000000\"  value=\"100\">100</option>"+
									"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
								"</select>"+foor_rpp+" </td>" +
								"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
								" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
								"</tbody></table></td></tr>";
					sumtext += text;
				}
		}
	}
	$("#showList tr").not($("#showList").find('.tablehead').parent()).remove();
	$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showCurrency(id)");
		$("#next").html("<a href=\"#\">"+foor_next+"&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showCurrency(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;"+foor_pre+"</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
}

function deleteCurrency(){
	waitingDialog({});
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){
			var thisid = $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value");
			if($('tr [type=checkbox]').not($('tr [type=checkbox]').first()).size() != 1){
				if($("#isDefault"+thisid).attr("value") == "true"){
					simpleDialog("Can't Delete Default Currency");
					return false;
				}else{
					listCheckBox += thisid+",";
				}
			}else{
				simpleDialog("Can't Delete All Currency");
				return false;
			}	
		}
	}
	if(listCheckBox == ""){
		return false;
	}else{
		var url = "/currencyService";
		var data = { func : "delCurrency",
				id : listCheckBox.substring(0,listCheckBox.length - 1),
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
			}
		simpleservice(url,data,showCurrencyList);
	}
}

function ChangeSetToDefault(id){
	waitingDialog({});
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	var url = "/currencyService";
	var data = { func : "editDefaultCurrency",
			id : id,
			offset: (line*pagePointer)-line,
			line: line,
			pagePointer: pagePointer
		}
	simpleservice(url,data,showCurrencyList);
}

function moveCurrency(SS1,SS2)
{
    var SelID='';
    var SelText='';
    SS1 = SS1[1];
    SS2 = SS2[1];
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
        if (SS1.options[i].selected == true)
        {
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);
            SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
        }
    }
    SelectSort(SS2);
}
function SelectSort(SelList)
{
    var ID='';
    var Text='';
    for (x=0; x < SelList.length - 1; x++)
    {
        for (y=x + 1; y < SelList.length; y++)
        {
            if (SelList[x].text > SelList[y].text)
            {
                // Swap rows
                ID=SelList[x].value;
                Text=SelList[x].text;
                SelList[x].value=SelList[y].value;
                SelList[x].text=SelList[y].text;
                SelList[y].value=ID;
                SelList[y].text=Text;
            }
        }
    }
}